package operation;

import book.Book;
import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-07
 * Time: 16:49
 */
public class DelOperation implements IOperation{

    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除图书的名字：");
        String bookname = scanner.nextLine();
        int size = bookList.getUedSize();
        int pos = -1;
        for (int i = 0; i < size; i++) {
            Book book = bookList.getPos(i);
            if (bookname.equals(book.getName())){
                pos = i;
                break;
            }
        }
        if (pos != -1) {
            Book[] books = bookList.getBooks();
            for (int i = pos; i < size; i++) {
                books[i] = books[i + 1];
            }
            bookList.setUedSize(size - 1);
            System.out.println("删除成功");
            return;
        }
        System.out.println("未找到删除图书");
    }
}
